package com.longtop.perform.ar.service;

import java.util.HashMap;
import java.util.List;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.StringUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.intelliweb.view.Record;

public class AssignService extends BaseService {
	public static final String BEAN_ID = "assignService";

	/**
	 * 依据条件，读取账号信息列表
	 * 
	 * @param value
	 *            查询的条件
	 * @param pageNo
	 *            查询结果的起始记录。
	 * @param size
	 *            查询结果的最大行数。如果不需要，则设置为-1
	 * @return
	 */
	public QueryResult loadAssign(QueryConditions queryConditions, int pageNo, int pageSize) {
		return this.queryForPageByIbatis(queryConditions, pageNo, pageSize);

	}

	public List loadAssignAll(String statementId, Object object) {
		return this.queryForListByIbatis(statementId, object);

	}

	/**
	 * 动态构造DATASET，自增长count记录数
	 * 
	 * @param context
	 * @param statementId
	 * @param paramValue
	 * @param dataset
	 * @param datasetLoadCommandId
	 */
	public void buildDatasetByAutoCount(IActionContext context, String statementId, Object paramValue, Dataset dataset, String datasetLoadCommandId) {
		this.queryForPageByIbatisAutoCount(statementId, paramValue, dataset, false);

		// 设置显示分配未分配：
		dataset.moveBeforeFirst();
		while (dataset.hasNext()) {
			dataset.moveNext();
			String resId = StringUtil.getString(dataset.getValue("resId"));
			if (StringUtil.isEmpty(resId)) {
				dataset.setValue("resId", "未分配");
			} else {
				dataset.setValue("resId", "已分配");
			}
		}
		// 下面这句最关键,这是将Dataset的结构传递回前台.
		dataset.setLoadDataCommand(datasetLoadCommandId);// 重新设置dataset的LoadCommand
		context.addCommandParameter("dsScript", dataset.toString(), "string");
	}

	/**
	 * 获取一级支行号
	 * 
	 * @param dataset_Org
	 */
	public void loadLevelOneOrg(Dataset masterDataset, Dataset dataset_Org, SessionObject sessionObject) {
		HashMap map = new HashMap();

		String orgId = sessionObject.getOrgId();
		String code = sessionObject.getOrgLevelCode();
		if (orgId.equals("00000")) {
			Record record = new Record();
			record.setValue("CODE", "-1");
			record.setValue("NAME", "全部");
			dataset_Org.addRecord(record);
			code = "-1";
		} else {
			code = code.substring(6, 11);
			map.put("code", code);
		}
		List orgList = this.queryForListByIbatis("arAssignSql.findLevelOrg", map);
		BeanUtil.listToDataset(orgList, dataset_Org, true);
		masterDataset.getField("fitOrgId").setDefaultValue(code);
	}

}
